package com.tf.integrated.config;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * 公共配置管理
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
public class Config
{
    private static String  mimgpath  = "/dataimagesb/";
    private static String  mproxypath  = "/dataimagesb";
    private static String  murlpath    = "http://recognize-frontend2-gcgs-cx-gc-20201009.ccsyun.vip/dataimages/";
    private static HashMap<String,String> mDevurl = new  HashMap<String,String>();
    private static HashMap<String,String> mDevToken = new  HashMap<String,String>();

    public static String getMproxypath()
    {
        return mproxypath;
    }

    public static String getMurlpath()
    {
        return murlpath;
    }

    public static String getMimgpath()
    {
        return mimgpath;
    }

    public static HashMap<String, String> getDevUrlMap() {
        return mDevurl;
    }

    public static void setDevurl(String devid, String url)
    {
        if(devid!=null&&devid.equals("")==false&&url!=null&&url.equals("")==false)
        {
            mDevurl.put(devid,url);
        }
    }

    public static void setDevToken(String devid,String token)
    {
        if(devid!=null&&devid.equals("")==false&&token!=null&&token.equals("")==false)
        {
            mDevToken.put(devid,token);
        }
    }

    public static HashMap<String, String> getmDevToken()
    {
        return mDevToken;
    }

    public static void setDevurloffline(String devid)
    {
        if(devid!=null&&devid.equals("")==false)
        {
            mDevurl.remove(devid);
            mDevToken.remove(devid);
        }
    }

    public static void setDevurloffhead(String devid)
    {
        if(devid!=null&&devid.equals("")==false)
        {
            mDevToken.remove(devid);
        }
    }

    public static void setMimgpath(String mimgpath) {
        Config.mimgpath = mimgpath;
    }

    public static void setMproxypath(String mproxypath) {
        Config.mproxypath = mproxypath;
    }

    public static void setMurlpath(String murlpath) {
        Config.murlpath = murlpath;
    }

    public static int readInt32BE(byte[] buf, int index)
    {
        int value = 0;
        byte[] bytes = new byte[4];
        for (int i = 0; i < 4; i++)
        {
            bytes[i] = buf[index+i];
        }
        for (int i = 0; i < 4; i++)
        {
            int shift = (4 - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;
        }
        return value;
    }

    public static String readString(byte[] buf,int index,int len)
    {
        String rval = null;
        byte[] bytes = new byte[len];
        for (int i = 0; i < len; i++)
        {
            bytes[i] = buf[index+i];
        }
        try
        {
            rval = new String(bytes,"utf-8");
        }
        catch (Exception e)
        {

        }
        return rval;
    }

    public static void saveResFile(String filename,ClassLoader clload)
    {
        FileOutputStream fileOutputStream = null;
        try
        {
            File file = new File(getMproxypath()+"/"+filename);
            if (!file.exists())
            {
                file.createNewFile();
            }
            else
            {
                file.delete();
                file.createNewFile();
            }
            fileOutputStream = new FileOutputStream(file);
            try
            {
                byte[] buff = new byte[256*1024];
                InputStream inputStream = clload.getResourceAsStream(filename);
                if(inputStream!=null)
                {
                    int readlen = inputStream.read(buff,0,256*1024);
                    while (readlen>0)
                    {
                        fileOutputStream.write(buff,0,readlen);
                        fileOutputStream.flush();
                        readlen = inputStream.read(buff,0,256*1024);
                    }
                    inputStream.close();
                }
            }
            catch (Exception e)
            {

            }
            fileOutputStream.close();
            file.canExecute();
        }
        catch (Exception e)
        {

        }
    }

    public static void saveFile(String tmppath,byte[] buff)
    {
        try
        {
            File file = new File(tmppath);
            if (!file.exists())
            {
                file.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(buff);
            fileOutputStream.flush();
            fileOutputStream.close();
        }
        catch (Exception e)
        {

        }
    }

    public static String getStarturlpre(String devid)
    {
        return mDevurl.get(devid);
    }

    public static String getmCurtoken(String devid)
    {
        return mDevToken.get(devid);
    }

    public static String buildMap(Map<String, String> map)
    {
        StringBuffer sb = new StringBuffer();
        if (map.size() > 0)
        {
            for (String key : map.keySet())
            {
                sb.append(key + "=");
                if (StringUtils.isEmpty(map.get(key)))
                {
                    sb.append("&");
                }
                else
                {
                    String value = map.get(key);
                    sb.append(value + "&");
                }
            }
        }
        return sb.toString();
    }

    public static String client(String url, HttpMethod method, String body, HttpHeaders header)
    {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(15*1000);
        requestFactory.setReadTimeout(15*1000);
        RestTemplate client = new RestTemplate(requestFactory);
        HttpEntity<String> requestEntity = new HttpEntity<String>(body, header);
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

    public static String clientWithFormData(String url, HttpMethod method, MultiValueMap<String, Object> param, HttpHeaders header)
    {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setConnectTimeout(15*1000);
        requestFactory.setReadTimeout(15*1000);
        RestTemplate client = new RestTemplate(requestFactory);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String,Object>>(param,header);
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }
    /**
     * url转变为 File对象
     * @param url
     * @param fileName
     * @return
     * @throws Exception
     */
    public static File createFile(String url, String fileName) throws Exception
    {
        File file = null;
        URL urlfile;
        InputStream inStream = null;
        OutputStream os = null;
        try {
            file = File.createTempFile("net_url", fileName);
            //下载
            urlfile = new URL(url);
            inStream = urlfile.openStream();
            os = new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
                if (null != inStream) {
                    inStream.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    public static int getChannelFromId(int channelId){
        return channelId-1;
    }
}